package com.whut.cristiano.autographbook.Activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.whut.cristiano.autographbook.Model.PhoneDialog;
import com.whut.cristiano.autographbook.R;
import com.whut.cristiano.autographbook.Util.ImageUtil;
import com.whut.cristiano.autographbook.Util.LogUtil;
import com.whut.cristiano.autographbook.Util.SharedPrefUtil;
import com.whut.cristiano.autographbook.Util.ToastUtil;
import com.yyx.beautifylib.model.BLPickerParam;
import com.yyx.beautifylib.model.BLResultParam;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class CustomActivity extends BaseActivity {

    private Button chooseDrawBtn;
    private Button submitDrawBtn;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom);
        initView();
    }

    @Override
    public void initView() {
        super.initView();
        chooseDrawBtn = (Button) findViewById(R.id.choose_draw_activity);
        chooseDrawBtn.setOnClickListener(this);
        submitDrawBtn = (Button) findViewById(R.id.submit_draw_activity);
        submitDrawBtn.setOnClickListener(this);
        imageView = (ImageView) findViewById(R.id.activity_custom_image);
    }
    private void gotoPhotoPickActivity() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            BLPickerParam.startActivity(CustomActivity.this);
        } else {
            EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问读写权限",1, perms);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case BLPickerParam.REQUEST_CODE_PHOTO_PICKER:
                BLResultParam param = data.getParcelableExtra(BLResultParam.KEY);
                List<String> imageList = param.getImageList();
                StringBuilder sb = new StringBuilder();
                for (String path:imageList){
                    sb.append(path);
//                    sb.append("\n");
                }
                if(sb==null){
                    break;
                }else {
                    LogUtil.d("OSSUtil",sb.toString());
                    SharedPrefUtil.saveTemplateOnePicUrl(sb.toString());
                    Glide.with(this).load(sb.toString()).centerCrop().into(imageView);
                    break;
                }

        }
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.choose_draw_activity:
                gotoPhotoPickActivity();
                break;
            case R.id.submit_draw_activity:
                PhoneDialog dialog = new PhoneDialog();
                dialog.show(getSupportFragmentManager(), "phoneDialog");
                break;
        }
    }


}
